<?php
class QianAction extends HomeAction {

    public function index(){         
        $beginDate = strtotime(date("Y-m-1"));
        $beginStr = date("Y-m-d",$beginDate);
        $endDate = strtotime("$beginStr +1 month -1 day");
        $dataArr = array();
        for ($i=$beginDate; $i <=$endDate ; $i+=24*3600) { 
            array_push($dataArr,date("m-d",$i));
        }
        $this->assign('date',$dataArr);

        $map['createTime'] = array('between',array($beginDate,$endDate));
        $map['memberID'] = $this->_user['memberID'];
        $sign = M('Sign')->where($map)->getField('id,signDate');
        if (in_array(date("Y-m-d"),$sign)) {
            $flag = '1';
        }else{
            $flag = '0';
        }
        foreach ($sign as $key => $value) {
            $sign[$key]=date("m-d",strtotime($value));
        }
        $this->assign('sign',$sign);
        $this->assign('flag',$flag);

        $signNumber = M('LotteryConfig')->getField('signNumber');
        $this->assign('signNumber',$signNumber);
        $this->display();       
    }

    public function sign(){        

        $date = date("Y-m-d");
        $map['memberID'] = $this->_user['memberID'];
        $map['signDate'] = $date;
        $list = M('Sign')->where($map)->find();
        if ($list) {            
            echo $this->echo_json_str('今天您已签过',$message,$url); die;
        }else{
            //昨天是否签到
            $yesterday = date("Y-m-d",strtotime("-1 day"));
            unset($map);
            $map['signDate'] = $yesterday;
            $map['memberID'] = $this->_user['memberID'];
            $list = M('Sign')->where($map)->find();
            if ($list) {
                if ($list['point']>=7) {
                    $point = 1;
                }else{
                    $point = $list['point']+1;
                }
            }else{
                $point = 1;
            }

            $data['signDate'] = $date;
            $data['point'] = $point;
            $data['memberID'] = $this->_user['memberID'];
            $data['createTime'] = time();
            $list = M('Sign')->add($data);
            if ($list) {
                M('member')->where('id='.$this->_user['memberID'])->setInc('point',$point);
                echo $this->echo_json_str('SUCCESS',$point,date("m-d")); die;
            }else{
                echo $this->echo_json_str('操作失败',$message,$url); die;
            }
        }
    } 
}